<?php

namespace app\demo\controller;

use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use my\JRedis;

class Test extends Base
{

    public function index()
    {
        $list2 = Db::table("frame_item")->where(true)->field("id,addr,code1,fs_id,ppe_id,frame_id,module_id,is_internal")->select()->toArray();
        $list1 = Db::connect("db2")->table("frame_item")->where(true)->field("id,addr,code1,fs_id,ppe_id,frame_id,module_id,is_internal")->select()->toArray();
        $m = [];
        foreach ($list1 as $key=>$row) {
            $m[$row['addr']] = $row;
        }
        $list3 = [];
        foreach ($list2 as $key=>$row) {
            if(!isset($m[$row['addr']]) && $row['code1'] !== '90') {
                $list3[] = $row;
            }
        }
        return successReturn([
            'count' => count($list3),
            'list' => $list3,
        ]);
        //        $now = date("Y-m-d H:i:s");
//        Cache::set("name","Jinag" . $now);
//        var_dump($now);
    }

    public function update_frame_item_code() {
        $list = Db::connect("stat")->table("frame_item")->where("input_method","=",1)->field("id,addr")->select()->toArray();
        foreach ($list as &$row) {
            $code_arr = explode("-",$row["addr"]);
            if(count($code_arr) !== 4) {
                return errorReturn("操作失败",$row);
            }
            Db::connect("stat")->table("frame_item")->where("id","=",$row['id'])->update([
                "code1" => $code_arr[0],
                "code2" => $code_arr[1],
                "code3" => $code_arr[2],
                "code4" => $code_arr[3],
            ]);
        }
        return successReturn($list);
    }

    private function config_list()
    {
        $param = request()->param();
        $frame_id = $param['frame_id'] ?? 1;
        $rs485_config = [
            "port_name" => "COM1",
            "baud_rate" => 9600,
            "data_bits" => 8,
            "stop_bits" => 1,
            "minimum_read_size" => 4,
        ];
        $modbus_rtu_config = [
            "port_name" => "COM1",
            "baud_rate" => 9600,
            "data_bits" => 8,
            "stop_bits" => 1,
            "minimum_read_size" => 4,
            "parity_mode" => 0,
            "connect_timeout" => 3000,
            "response_timeout" => 1000
        ];
        $modbus_tcp_config = [
            "mode" => "client",
            "ip" => "192.168.1.15",
            "port" => "512",
            "connect_timeout" => 3000,
            "response_timeout" => 1000
        ];
        $tcp_config = [
            "mode" => "client",
            "ip" => "192.168.1.15",
            "port" => "9988",
            "connect_timeout" => 3000,
            "response_timeout" => 1000,
        ];


        // Db::connect('stat')->table('protocol')->where('id', 'in', [2, 3])->update(['config' => json_encode($rs485_config)]);
        // Db::connect('stat')->table('protocol')->where('id', 'in', [4, 6])->update(['config' => json_encode($tcp_config)]);
        // Db::connect('stat')->table('protocol')->where('id', 'in', [5])->update(['config' => json_encode($modbus_rtu_config)]);

        
        $frame_item_list = Db::connect('stat')->table('frame_item')->where('frame_id', '=', $frame_id)->select()->toArray();
        foreach ($frame_item_list as &$item) {
            $item['ratio'] = floatval($item['ratio']);
        }

        // $frame_item_str = file_get_contents('tmp/p.json');
        // $frame_item_list = json_decode($frame_item_str, true);

        return json([
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'rs485_config' => json_encode($rs485_config),
                'modbus_rtu_config' => json_encode($modbus_rtu_config),
                'modbus_tcp_config' => json_encode($modbus_tcp_config),
                'tcp_config' => json_encode($tcp_config),
                'frame_item_list' => json_encode($frame_item_list, JSON_UNESCAPED_UNICODE),
                // 'frame_item_list' => $frame_item_list,
            ]
        ]);
    }

    private function variable_queueu()
    {
        // $str = "{\"data_type\":\"int\",\"spec_type\":\"uint8\",\"addr\":\"52-00-01-001\",\"field_name\":\"\xe9\xa3\x8e\xe9\x80\x9f\xe4\xbc\xa0\xe6\x84\x9f\xe5\x99\xa8\xe9\x80\x9a\xe8\xae\xaf\xe7\x8a\xb6\xe6\x80\x81\",\"Value\":1,\"create_time\":\"2025-02-17 17:16:20\"}";
        // $res = json_decode($str, true);
        // var_dump($res);
        // $param = request()->param();
        // $key = $param['key'] ?? '52-00-01-001';
        // $list = JRedis::lRange($key,0,100);
        // $list = array_reverse($list);
        // foreach ($list as $k => &$v) {
        //     $list[$k] = json_decode($v, true);
        // }
        // return json([
        //     'code' => 0,
        //     'msg' => 'success',
        //     'data' => [
        //         'list' => $list,
        //     ]
        // ]);

        // $list = Db::table('test')->select()->toArray();
        // pp($list);

//        $res =  Db::table('test')->insert(['aaa'=>9876543210123456789.123]);
//        var_dump($res);
    }

    // 序号和通道号改成从1开始
    private function fix_module_number() {
        //        $list  = Db::table("frame_item")->where("fs_id",">",0)->select()->toArray();
//        $affect_rows = 0;
//        foreach ($list as $key=>$row) {
//            $code3 = intval($row['code3'])+1;
//            $code4 = intval($row['code4'])+1;
//
//            $code3New = str_pad($code3,2,"0",STR_PAD_LEFT);
//            $code4New = str_pad($code4,3,"0",STR_PAD_LEFT);
//            $addr = $row['code1'] . "-" . $row['code2'] . "-". $code3New."-".$code4New;
//            if(intval($row['code2']) < 5) {
//                $field_name = "通道 " . $addr;
//                $update_data = [
//                    'code3' => $code3New,
//                    'code4' => $code4New,
//                    'addr' => $addr,
//                    'field_name' => $field_name,
//                ];
//            }else {
//                $update_data = [
//                    'code3' => $code3New,
//                    'code4' => $code4New,
//                    'addr' => $addr,
//                ];
//            }
//            $affect_rows += Db::table("frame_item")->where("id","=",$row['id'])->update($update_data);
//        }
//        var_dump($affect_rows);
    }

}
